package com.dxyxx.framework;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.dxyxx.R;

/**
 * 券券在手
 * 
 * @author Administrator
 * 
 */
public class QuanActivity extends FragmentActivity {

	private int index = 0;

	/**
	 * 用于对Fragment进行管理
	 */
	private FragmentManager fragmentManager;

	/** tabs */
	private GroupOrderFragment mTab01;// 団券
	private TicketFragment mTab02;// 金券

	/** btns */
	private TextView group_btn;// 団券
	private TextView ticket_btn;// 金券

	public void onCreate(Bundle savedInstanceState) {
		Log.i("hello", "work onCreateView");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quan);
		fragmentManager = getSupportFragmentManager();
		initUI();
		setTabSelection(index);

	}

	public void initUI() {
		ImageView back = (ImageView) findViewById(R.id.backImageButton);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});
		// two btns
		group_btn = (TextView) findViewById(R.id.job);// 兼职
		ticket_btn = (TextView) findViewById(R.id.intern);// 实习

		// 点击兼职按钮
		group_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				setTabSelection(0);
			}
		});

		// 点击实习按钮
		ticket_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				setTabSelection(1);
			}
		});

	}

	/**
	 * 根据index跳转到不同的tab. index = 0 ： 兼职 index = 1 ：实习
	 * 
	 * @param index
	 */
	// 严重bug。。 textView 只有 api16才有setbackground
	public void setTabSelection(int index) {
		Log.i("hello", "work set tab");
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		hideFragments(transaction);
		switch (index) {
		case 0:
			group_btn.setBackgroundResource(R.drawable.yd_work_selected);
			group_btn.setTextColor(getResources().getColor(android.R.color.white));
			ticket_btn.setBackgroundResource(R.drawable.yd_work_unselected);
			ticket_btn.setTextColor(0xffcccccc);
			if (mTab01 == null) {
				mTab01 = new GroupOrderFragment();
				transaction.add(R.id.work_content, mTab01);
			} else {
				transaction.show(mTab01);
			}

			break;
		case 1:
			ticket_btn.setBackgroundResource(R.drawable.yd_work_selected);
			ticket_btn.setTextColor(getResources().getColor(android.R.color.white));
			group_btn.setBackgroundResource(R.drawable.yd_work_unselected);
			group_btn.setTextColor(0xffcccccc);
			if (mTab02 == null) {
				mTab02 = new TicketFragment();
				transaction.add(R.id.work_content, mTab02);
			} else {
				transaction.show(mTab02);
			}
			break;
		}
		transaction.commit();
	}

	/**
	 * 将所有的Fragment都置为隐藏状态.
	 */
	private void hideFragments(FragmentTransaction transaction) {
		if (mTab01 != null) {
			transaction.hide(mTab01);
		}
		if (mTab02 != null) {
			transaction.hide(mTab02);
		}
	}

}
